package com.example.meterialproject.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.example.meterialproject.R
import com.example.meterialproject.bean.TransitionBean
import com.example.meterialproject.exts.click
import com.example.meterialproject.exts.szj.szj
import kotlinx.coroutines.ExperimentalCoroutinesApi

/**
 *
 * @ClassName: TransitionAdapter
 * @Author: android 超级兵
 * @CreateDate: 4/26/22$ 9:30 AM$
 * TODO
 */
class TransitionAdapter(
    val context: Context,
    private val mDataList: List<TransitionBean>,
) : RecyclerView.Adapter<TransitionAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        LayoutInflater.from(context).inflate(R.layout.item_transition, parent, false).apply {
            return ViewHolder(this)
        }
    }

    @ExperimentalCoroutinesApi
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val url = mDataList[position].imageUrl
        val content = mDataList[position].content

        // file:///android_asset/水果/凤梨.png
        holder.imageHead.apply {
            // 加载图片
            load(url)

            setBackgroundColor(context.szj.randomColor((255 * 0.4).toInt()))
        }

        holder.tvContent.text = content

        // * 设置 TransitionName
        ViewCompat.setTransitionName(holder.itemView, url)

        holder.itemView.click {
            block?.block(it, url, position)
        }
    }


    override fun getItemCount(): Int = mDataList.size

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageHead: AppCompatImageView by lazy {
            itemView.findViewById(R.id.imageHead)
        }
        val tvContent: AppCompatTextView by lazy {
            itemView.findViewById(R.id.tvContent)
        }
    }

    fun interface CallBackListener {
        fun block(view: View, url: String, position: Int)
    }

    var block: CallBackListener? = null

}